package net.bolue.console.model;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.bolue.console.data.DataBean;
import net.bolue.console.data.InsertInfo;
import net.bolue.console.data.MDBOpInfo;
import net.bolue.console.data.UpdateInfo;
import net.bolue.console.data.model.InsertInfoModel;
import net.bolue.console.data.model.UpdateInfoModel;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.InsResModel;
import net.bolue.sysmng.model.UpdResModel;
import net.bolue.sysmng.tblbean.InsRes;
import net.bolue.sysmng.tblbean.MdbopRes;
import net.bolue.sysmng.tblbean.MdbopSubRes;
import net.bolue.sysmng.tblbean.UpdRes;
import net.bolue.web.entry.FormTable;
import net.bolue.web.mvc.ActionResult;
import net.bolue.web.util.FormUtil;

public class MDBOpModel extends BusiBase{

	private MDBOpInfo mDBOpInfo;
	
	public MDBOpModel(){
		
	}
	
	public MDBOpModel(HttpServletRequest request,MDBOpInfo mDBOpInfo){
		super(request);
		this.mDBOpInfo = mDBOpInfo;
	}
	
	public ActionResult insert()throws BolueException{
		MdbopRes mdbopRes = mDBOpInfo.getMdbopRes();
		if(mdbopRes.getFormType()==1){
			return insertNormal();
		}
		return null;
	}
	
	/**
	 * 普通表单插入
	 * @return
	 * @throws BolueException
	 */
	public ActionResult insertNormal()throws BolueException{
		Map<String, DataBean> dbopResMap = mDBOpInfo.getDbopResMap();
		if(dbopResMap==null||dbopResMap.size()<1){
			return null;
		}
		Set<String> keySet = dbopResMap.keySet();
		Iterator<String> keyIt = keySet.iterator();
		while(keyIt.hasNext()){
			String subResId = keyIt.next();
			DataBean dbopRese = dbopResMap.get(subResId);
			if(dbopRese instanceof InsertInfo){
				InsertInfo insertInfo = (InsertInfo)dbopRese;
				InsertModel insertModel = new InsertModel(this.request,insertInfo,subResId);
				insertModel.insert();
			}else if(dbopRese instanceof UpdateInfo){
				UpdateInfo updateInfo = (UpdateInfo)dbopRese;
				UpdateModel updateModel = new UpdateModel(this.request,updateInfo,subResId);
				updateModel.update();
			}
		}
		return null;
	}
}
